package com.hundsun.fcp.activity;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.hundsun.fcp.R;
import com.hundsun.fcp.adapter.ActionCenterAdapter;
import com.hundsun.fcp.base.BaseActivity;
import com.hundsun.fcp.bean.ActionCenter;
import com.hundsun.fcp.common.AppointFiled.AdvflagDef;
import com.hundsun.fcp.common.Redirect;
import com.hundsun.fcp.common.utils.ToastUtil;
import com.hundsun.fcp.openapi.IOpenApiDataServiceCallback;
import com.hundsun.fcp.openapi.trade.TradeActionCenterDataService;
import com.hundsun.fcp.openapi.trade.bean.ResponseActionBean;
import com.hundsun.fcp.openapi.trade.bean.Result;
import com.hundsun.fcp.widget.pulltorefresh.PullToRefreshBase;
import com.hundsun.fcp.widget.pulltorefresh.PullToRefreshBase.OnRefreshListener;
import com.hundsun.fcp.widget.pulltorefresh.PullToRefreshListView;



public class MineActionCenterActivity extends BaseActivity implements IOpenApiDataServiceCallback<Result>{
	/** 活动列表 */
	private PullToRefreshListView mActivitiesLv;
	/** 列表数据适配 */
	private ActionCenterAdapter mAdapter;
	
	private TradeActionCenterDataService mService ;

	protected int getContentLayout() {
		return R.layout.fcp_mine_action_center;
	}

	@Override
	protected void initHeader() {
		super.initHeader();
		mHeaderMenu.setTitle(R.string.mine_action_center);
	}

	@Override
	public void initView() {
		super.initView();
		mService = new TradeActionCenterDataService(this) ;
		mService.setCallback(this) ;
		mActivitiesLv = (PullToRefreshListView) findViewById(R.id.lv_activities) ;
		mActivitiesLv.getLoadingLayoutProxy().setPullLabel(getString(R.string.pull_to_refresh_pull_down_label));
		mAdapter = new ActionCenterAdapter(this) ;
		mActivitiesLv.setAdapter(mAdapter) ;
		
		mActivitiesLv.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				actionList() ;
			}
		}) ;
		
		mActivitiesLv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				ActionCenter actionCenter = mAdapter.getItem(position-mActivitiesLv.getRefreshableView().getHeaderViewsCount()) ;
				 String type = actionCenter.getType() ;
					//根据图片类型跳转
					 if(AdvflagDef.ACTION_HTML.equals(type)){
						Redirect.startWebView(MineActionCenterActivity.this, actionCenter.getLinkurl(),actionCenter.getDescribe()) ;
					}
			}
		}) ;
		 
		actionList() ;
	}
	private void actionList(){
		mService.cancel() ;
		mService.get(null) ;
	}

	@Override
	public void onGetData(Result data) {
		if(data.isSuccess()){
			ResponseActionBean actionBean = (ResponseActionBean) data ;
			mAdapter.clear() ;
			mAdapter.addData(actionBean.list) ;
		}else{
			ToastUtil.showInfoToast(this, data.info,ToastUtil.LONG) ;
		}
		mActivitiesLv.onRefreshComplete() ;
	}

	@Override
	public void onGetError(int errorCode, String errorMsg, Throwable error) {
		mActivitiesLv.onRefreshComplete() ;
		ToastUtil.showInfoToast(this, errorMsg,ToastUtil.LONG) ;
	}
}
